﻿Public Partial Class Intra
    Inherits System.Web.UI.Page
    Public script As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            EncheMenu()
        End If
    End Sub

    Private Sub VerificaAssociado()
        Dim fun As New Funcoes
        Try
            If ConfigurationManager.AppSettings("LoginAssociado") = "SIM" Or Request("Login") = "Sim" Then

                If IsNothing(Session("codigoassociado")) Then
                    tb_Login.Visible = True
                    tb_dadosAssociado.Visible = False
                Else
                    Dim ws As New AssociadoWS.Associado
                    tb_Login.Visible = False
                    tb_dadosAssociado.Visible = True
                    lbl_Associado.Text = "Olá: " & ws.DadosAssociado(txt_Matricula.Text).Rows(0)("Nome")
                    'lbl_Email.Text = "Olá: " & ws.DadosAssociado(txt_Matricula.Text).Rows(0)("Email")

                End If
            Else
                tb_Login.Visible = False
                tb_dadosAssociado.Visible = False
            End If
        Catch ex As Exception
            script += fun.MsgboxAviso(ex.Message)
        End Try
    End Sub

    Private Sub btn_Acessar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_Acessar.Click
        'virificar se os dados informado esta corretos
        Dim fun As New Funcoes
        Dim ws As New AssociadoWS.Associado
        Try

            If ws.AutenticarAssociado(txt_Matricula.Text, txt_senha.Text) Then
                Session("codigoassociado") = txt_Matricula.Text
                Centro.Attributes("src") = "Associado.aspx"
            Else
                Throw New Exception("Dados inválidos")
            End If
            VerificaAssociado()

        Catch ex As Exception
            script += fun.MsgboxAviso(ex.Message)
        End Try

    End Sub

    Private Sub lb_Sair_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lb_Sair.Click
        Session("codigoassociado") = Nothing
        Response.Redirect("Default.aspx")
        VerificaAssociado()
    End Sub


    Private Sub EncheMenu()
        Dim c As New SiteDLL.Menu
        ' c.addWhere("Topo", ClasseBase.Classes.PersistClass.TipoFiltro.Igual, "Não")
        Dim dt As DataTable = c.getTodosRegistros
        Try
            c.IDMenu = 1
            c.carregar()
            lblAcesso.Text = "Acessos: " & c.Acessos

            Dim drs() As DataRow = dt.Select("IDPai = 0")
            For Each dr As DataRow In drs
                Dim m As New MenuItem
                m.Value = dr("IDMenu")
                m.Text = dr("Nome")
                m.Target = dr("Target") & ""
                If IsNothing(dr("Url")) Then
                    m.NavigateUrl = "Pagina.aspx?IDPagina=11"
                Else
                    m.NavigateUrl = "ContaClick.aspx?IDMenu=" & dr("IDMenu") & "&Link=" & dr("Url")
                End If

                EncheSub(dt, m)
                Menu1.Items.Add(m)
            Next
        Catch ex As Exception

        Finally
            c.Dispose()
            c = Nothing
            dt.Dispose()
            dt = Nothing
        End Try

    End Sub

    Private Sub EncheSub(ByVal dt As DataTable, ByVal menu As MenuItem)
        Dim drs() As DataRow = dt.Select("IDPai = " & menu.Value)
        For Each dr As DataRow In drs
            Dim m As New MenuItem
            m.Value = dr("IDMenu")
            m.Text = dr("Nome")
            m.Target = dr("Target") & ""
            If IsNothing(dr("Url")) Then
                m.NavigateUrl = "Pagina.aspx?IDPagina=11"
            Else
                m.NavigateUrl = "ContaClick.aspx?IDMenu=" & dr("IDMenu") & "&Link=" & dr("Url")
            End If
            EncheSub(dt, m)
            menu.ChildItems.Add(m)
        Next
    End Sub

End Class